#include "mpLog.h"
#include "mpFileUtils.h"
#include "mpLocaleUtils.h"
#include "mpFileUtilsTests.h"

#include <boost/filesystem.hpp>


mpFileUtilsTests::mpFileUtilsTests()
{
}
void mpFileUtilsTests::initTestCase(){}
void mpFileUtilsTests::cleanupTestCase(){}

void mpFileUtilsTests::case_getFileTextContent(){
#ifdef TEST_STRATEGY_COMPLETED

    boost::filesystem::path tmp = boost::filesystem::initial_path<boost::filesystem::path>();
    std::string strPath = tmp.string();

    MPLOG_INFO << (MP::LocaleUtils::check_is_utf8(strPath) ? "utf8":"gbk");

    MPLOG_INFO
            << "initial_path : "
            << boost::filesystem::initial_path<boost::filesystem::path>().string();

    MPLOG_INFO
            << "current_path : "
            << boost::filesystem::current_path().string();


    std::string path = MP::FileUtils::get_appdir() + PATH_DELIM + "uft8.txt";
    std::string content = "";

    //std::string content = MP::FileUtils::getFileTextContent()
    QVERIFY2(MP::FileUtils::get_file_content(path,content),"get file [utf8.txt] text content");
    QVERIFY2(content == "一日不见兮，思之如狂。 凤飞翱翔兮，四海求凰。One day's absence makes me think like a maniac. The Phoenix is flying, seeking a mate from all over the world.",
             "compare file [utf8.txt] text content");

#endif
}

void mpFileUtilsTests::case_writeFileTextContent(){
#ifdef TEST_STRATEGY_COMPLETED

    std::string path = MP::FileUtils::get_appdir() + PATH_DELIM + "write.txt";
    std::string content1 = "ABCDEFG";
    std::string content2 = "1234567";
    std::string content3 = "!@#$%^&中文!@#$%^&";
    std::string tmp = "";

    MP::FileUtils::set_file_content(path,content1);
    MP::FileUtils::get_file_content(path,tmp);

    QVERIFY2(tmp == content1,"case_writeFileTextContent 1");

    MP::FileUtils::set_file_content(path,content2,false);
    MP::FileUtils::get_file_content(path,tmp);
    QVERIFY2(tmp == "ABCDEFG1234567","case_writeFileTextContent 2");

    MP::FileUtils::set_file_content(path,content3);
    MP::FileUtils::get_file_content(path,tmp);
    QVERIFY2(tmp == content3,"case_writeFileTextContent 3");


#endif

}

void mpFileUtilsTests::case_file_op(){
#ifdef TEST_STRATEGY_COMPLETED


    std::string dir = MP::FileUtils::get_appdir() + PATH_DELIM + "tests_files_op";
    std::string dir_son1 = dir + PATH_DELIM + "son1";
    std::string dir_son2 = dir + PATH_DELIM + "son2";
    std::string dir_son1_son1 = dir_son1 + PATH_DELIM + "son1";

    std::string file = dir + PATH_DELIM + "test.txt";
    std::string file_copy = dir + PATH_DELIM + "test_copy.txt";
    std::string file_move = dir + PATH_DELIM + "test_move.txt";
    std::string file_rename = dir + PATH_DELIM + "test_rename.txt";

    MP::FileUtils::remove_file(file);
    MP::FileUtils::remove_dir(dir);

    QVERIFY2(!MP::FileUtils::check_file_isexist(file),"file test.txt is not exist");
    QVERIFY2(!MP::FileUtils::check_dir_isexist(dir),"dir tests_files_op is not exist");
    QVERIFY2(MP::FileUtils::create_directories(dir),"create dir tests_files_op");

    QVERIFY2(MP::FileUtils::create_directories_forfile(file),"create dir for file test.txt");
    QVERIFY2(MP::FileUtils::create_directories(dir_son2),"create dir tests_files_op/son2");
    QVERIFY2(MP::FileUtils::create_directories(dir_son1_son1),"create dir tests_files_op/son1/son1");

    QVERIFY2(MP::FileUtils::check_dir_access(dir),"check dir tests_files_op access");
    QVERIFY2(MP::FileUtils::touch_file(file),"create file test.txt");

    QVERIFY2(MP::FileUtils::get_file_name(file) == "test.txt","get file test.txt name");
    QVERIFY2(MP::FileUtils::get_file_name(file,false) == "test","get file test.txt name");
    QVERIFY2(MP::FileUtils::get_file_size(file) == 0,"get file test.txt size");

    std::string strbirthtime;
    MP::FileUtils::get_file_birthtime(file,strbirthtime);
    MPLOG_INFO << "[getFileBirthtime]:" << strbirthtime;

    MP::FileUtils::copy_file(file,file_copy,true);
    QVERIFY2(MP::FileUtils::check_file_isexist(file),"after copyfile,from file is exist");
    QVERIFY2(MP::FileUtils::check_file_isexist(file_copy),"after copyfile,from file_copy is exist");

    MP::FileUtils::move_file(file,file_move,true);
    QVERIFY2(!MP::FileUtils::check_file_isexist(file),"after movefile,from file is not exist");
    QVERIFY2(MP::FileUtils::check_file_isexist(file_move),"after movefile,from file_move is exist");

    MP::FileUtils::rename_file(file_move,file_rename);
    QVERIFY2(!MP::FileUtils::check_file_isexist(file_move),"after renamefile,from file is not exist");
    QVERIFY2(MP::FileUtils::check_file_isexist(file_rename),"after renamefile,from file_rename is exist");

    QVERIFY2(!MP::FileUtils::remove_file(file),"remove file,file is not exist");
    QVERIFY2(MP::FileUtils::remove_file(file_rename),"remove file,file is exist");

    MP::FileUtils::touch_file(file);
    QVERIFY2(MP::FileUtils::clear_dir(dir),"clear dir tests_files_op");
    QVERIFY2(MP::FileUtils::remove_dir(dir),"clear dir tests_files_op");


#endif
}

//getCurrentAppDirectory,getCurrentUserAppDataDirectory,getAllUserAppDataDirectory,getCurrentDesktopDirectory
void mpFileUtilsTests::case_getDir(){

#ifdef TEST_STRATEGY_COMPLETED


    std::string getCurrentAppDirectory = MP::FileUtils::get_appdir();
    std::string getCurrentUserAppDataDirectory = MP::FileUtils::get_current_userdata_dir();
    std::string getAllUserAppDataDirectory = MP::FileUtils::get_all_userdata_dir();
    getAllUserAppDataDirectory = MP::FileUtils::get_all_userdata_dir();
    std::string getCurrentDesktopDirectory = MP::FileUtils::get_desktopdir();

    MPLOG_INFO << "[getCurrentAppDirectory]:" << getCurrentAppDirectory;
    MPLOG_INFO << "[getCurrentUserAppDataDirectory]:" << getCurrentUserAppDataDirectory;
    MPLOG_INFO << "[getAllUserAppDataDirectory]:" << getAllUserAppDataDirectory;
    MPLOG_INFO << "[getCurrentDesktopDirectory]:" << getCurrentDesktopDirectory;

    QVERIFY2(!getCurrentAppDirectory.empty(),"getCurrentAppDirectory is not empty");
    QVERIFY2(!getCurrentUserAppDataDirectory.empty(),"getCurrentUserAppDataDirectory is not empty");
    QVERIFY2(!getAllUserAppDataDirectory.empty(),"getAllUserAppDataDirectory is not empty");
    QVERIFY2(!getCurrentDesktopDirectory.empty(),"getCurrentDesktopDirectory is not empty");

    QVERIFY2(getCurrentAppDirectory != getCurrentUserAppDataDirectory,"getCurrentAppDirectory != getCurrentUserAppDataDirectory");
    QVERIFY2(getCurrentAppDirectory != getAllUserAppDataDirectory,"getCurrentAppDirectory != getAllUserAppDataDirectory");
    QVERIFY2(getCurrentAppDirectory != getCurrentDesktopDirectory,"getCurrentAppDirectory != getCurrentDesktopDirectory");

#endif
}
